package com.ylbh.modules.coupon.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ylbh.common.pojo.coupon.dto.*;
import com.ylbh.common.pojo.coupon.vo.*;
import com.ylbh.common.utils.PageUtils;
import com.ylbh.modules.business.entity.ShoppingBusinessGoods;
import com.ylbh.modules.coupon.mapper.CouponMapper;
import com.ylbh.modules.coupon.service.CouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * 优惠券
 * @author czx
 * @date 2019-05-10
 */
@Service
public class CouponServiceImpl extends ServiceImpl<CouponMapper, ShoppingBusinessGoods> implements CouponService {

    @Autowired
    private CouponMapper couponMapper;

    /**
     * 优惠券使用记录
     * @param usageRecordDTO
     * @return
     */
    @Override
    public PageUtils queryUsageRecordList(UsageRecordDTO usageRecordDTO) {
        Page<Object> page = new Page<>(usageRecordDTO.getPage(), usageRecordDTO.getLimit());
        List<UsageRecordVO> recordList = couponMapper.selectUsageRecordList(usageRecordDTO, page);
        return new PageUtils(new Page<UsageRecordVO>().setTotal(page.getTotal()).setRecords(recordList));
    }

    /**
     * 超级大玩家开通记录
     * @param openingRecordDTO
     * @return
     */
    @Override
    public PageUtils queryOpeningRecordList(OpeningRecordDTO openingRecordDTO) {
        Page<Object> page = new Page<>(openingRecordDTO.getPage(), openingRecordDTO.getLimit());
        List<OpeningRecordVO> recordList = couponMapper.selectOpeningRecordList(openingRecordDTO, page);
        return new PageUtils(new Page<OpeningRecordVO>().setTotal(page.getTotal()).setRecords(recordList));
    }

    /**
     * 优惠券领取记录
     * @param collectionRecordsDTO
     * @return
     */
    @Override
    public PageUtils queryCollectionRecordsList(CollectionRecordsDTO collectionRecordsDTO) {
        Page<Object> page = new Page<>(collectionRecordsDTO.getPage(), collectionRecordsDTO.getLimit());
        List<CollectionRecordsVO> recordsList = couponMapper.selectCollectionRecordsList(collectionRecordsDTO, page);
        return new PageUtils(new Page<CollectionRecordsVO>().setTotal(page.getTotal()).setRecords(recordsList));
    }

    /**
     * 优惠券信息列表
     * @param couponListDTO
     * @return
     */
    @Override
    public PageUtils queryCouponList(CouponListDTO couponListDTO) {
        Page<Object> page = new Page<>(couponListDTO.getPage(), couponListDTO.getLimit());
        List<CouponListVO> couponList = couponMapper.selectCouponList(couponListDTO, page);
        int unauditedCount = couponMapper.selectUnauditedCount();
        CouponVO vo = new CouponVO();
        vo.setList(couponList);
        vo.setCount(unauditedCount);
        return new PageUtils(new Page<CouponVO>().setTotal(page.getTotal()).setRecords(Collections.singletonList(vo)));
    }

    /**
     * 优惠券的详情
     * @param goodsId
     * @return
     */
    @Override
    public CouponDetailsVO queryCouponDetailsById(Long goodsId) {
        CouponDetailsVO details = couponMapper.selectCouponDetailsById(goodsId);
        // 查询商家的地址
        CouponAddressVO address = couponMapper.selectAddress(details.getUserId());
        if ("".equals(String.valueOf(address.getAreaId())) || !"null".equals(String.valueOf(address.getAreaId()))) {
            details.setAddress(address.getAreaName() + "-" + address.getArea() + "-" + address.getAr() + "-" + address.getAddress());
        } else {
            details.setAddress(address.getAddress());
        }
        return details;
    }

    /**
     * 优惠券商品上下架
     * @param goodsId
     * @param isSale
     */
    @Override
    public void modifyUpperAndLowerFrames(String goodsId, String isSale) {
        couponMapper.updateUpperAndLowerFrames(goodsId, isSale);
    }

    /**
     * 优惠券商品审核
     * @param couponPassDTO
     */
    @Override
    public void modifyAdoptById(CouponPassDTO couponPassDTO) {
        couponMapper.updateAdoptById(couponPassDTO);
    }
}
